package com.hlj.ncp.warehouse.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.hlj.ncp.common.core.domain.AjaxResult;
import com.hlj.ncp.warehouse.entity.Warehouse;
import com.hlj.ncp.warehouse.service.IWarehouseService;

/**
 * 仓库信息操作处理
 */
@Controller
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private IWarehouseService warehouseService;

    /**
     * 仓库管理页面
     */
    @GetMapping("/index")
    public String index() {
        return "warehouse/index";
    }

    /**
     * 获取仓库列表
     */
    @PreAuthorize("hasAuthority('warehouse:warehouse:list')")
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(Warehouse warehouse) {
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
        return AjaxResult.success(list);
    }

    /**
     * 获取仓库总数
     */
    @GetMapping("/count")
    @ResponseBody
    public AjaxResult count() {
        long count = warehouseService.count();
        return AjaxResult.success((int)count);
    }

    /**
     * 获取仓库详细信息
     */
    @PreAuthorize("hasAuthority('warehouse:warehouse:query')")
    @GetMapping(value = "/{warehouseId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("warehouseId") Long warehouseId) {
        return AjaxResult.success(warehouseService.selectWarehouseById(warehouseId));
    }

    /**
     * 新增仓库
     */
    @PreAuthorize("hasAuthority('warehouse:warehouse:add')")
    @PostMapping
    @ResponseBody
    public AjaxResult add(@RequestBody Warehouse warehouse) {
        return AjaxResult.success(warehouseService.insertWarehouse(warehouse));
    }

    /**
     * 修改仓库
     */
    @PreAuthorize("hasAuthority('warehouse:warehouse:edit')")
    @PutMapping("/{warehouseId}")
    @ResponseBody
    public AjaxResult edit(@PathVariable("warehouseId") Long warehouseId, @RequestBody Warehouse warehouse) {
        warehouse.setWarehouseId(warehouseId);
        return AjaxResult.success(warehouseService.updateWarehouse(warehouse));
    }

    /**
     * 删除仓库
     */
    @PreAuthorize("hasAuthority('warehouse:warehouse:remove')")
    @DeleteMapping("/{warehouseIds}")
    @ResponseBody
    public AjaxResult remove(@PathVariable Long[] warehouseIds) {
        return AjaxResult.success(warehouseService.deleteWarehouseByIds(warehouseIds));
    }
} 